package com.s666.demo.controller;

import com.s666.demo.service.RocketMQService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * rocket 消息队列的使用, 支持事务消息
 * @author lijin
 */
@Controller
@RequestMapping("/rocketmq")
@Slf4j
public class RocketMQController extends BaseController {

    private static final String TOPIC_NAME = "TopicTest";

    @Resource
    @Qualifier("RocketMQService")
    private RocketMQService rocketMQService;

    @Resource
    @Qualifier("RocketMQ2Service")
    private RocketMQService rocketMQ2Service;

    // /rocketmq/send?msg="我是普通消息"
    @RequestMapping("/send")
    @ResponseBody
    public String send(@RequestParam(value = "msg", required = true, defaultValue="我是普通消息") String msg) {
        rocketMQService.product(TOPIC_NAME, msg);
        return msg;
    }

    // /rocketmq/send_transaction?msg="我是事务消息"
    @RequestMapping("/send_transaction")
    @ResponseBody
    public String createOrderWithTransaction(@RequestParam(value = "msg", required = true, defaultValue="我是事务消息") String msg) {
        rocketMQService.createOrderWithTransaction(TOPIC_NAME, msg);
        rocketMQ2Service.createOrderWithTransaction(TOPIC_NAME, msg);
        return msg;
    }
}
